package com.yuanchu.search.listener;

import com.yuanchu.search.client.FeignOrderClient;
import com.yuanchu.search.dto.OrderDetailDto;
import com.yuanchu.search.entity.YoshopOrderRefundEntity;
import com.yuanchu.search.service.RefreshOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static com.yuanchu.search.listener.MqOrderConstants.*;

/**
 * @version: 1.0
 * @Author: adan
 * @description:
 * @date: 2024-02-23 17:08
 */
@Slf4j
@Component
public class OrderListener {



    @Resource
    private RefreshOrderService refreshOrderService;

    @Resource
    private FeignOrderClient feignOrderClient;

    /**
     * 监听订单新增或修改的业务
     * @param message
     */
    @RabbitListener(queues = ORDER_INSERT_QUEUE)
    public void listenOrderInsertOrUpdate(String message){
        OrderDetailDto orderDetail = feignOrderClient.queryStoreOrderDetail(Integer.valueOf(message));
        refreshOrderService.refreshOrder(orderDetail, message);
    }

    /**
     * 监听订单删除的业务
     * @param message
     */
    @RabbitListener(queues = ORDER_DELETE_QUEUE)
    public void listenOrderDelete(String message){
        refreshOrderService.removeOrder(Long.parseLong(message));
    }

    /**
     * 监听订单新增或修改的业务
     * @param message
     */
    // @RabbitListener(queues = ORDER_REFUND_QUEUE)
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = ORDER_REFUND_QUEUE),
            exchange = @Exchange(name = ORDER_REFUND_EXCHANGE),
            key = {ORDER_INSERT_KEY}
    ))
    public void listenOrderRefund(String message){
        YoshopOrderRefundEntity orderRefund = feignOrderClient.queryOrderRefundDetail(Integer.valueOf(message));
        refreshOrderService.refreshOrderRefund(orderRefund, message);
    }
}
